//
//  RequestListBaseData.m
//  QinjinProject
//
//  Created by Michael on 14-9-13.
//  Copyright (c) 2014年 ___Michael___. All rights reserved.
//

#import "RequestListBaseData.h"

@implementation RequestListBaseData

-(id)init
{
    if (self = [super init])
    {
        self.dataArray = [NSMutableArray array];
        self.strLastId = @"0";
        self.requestPage = kLoadMoreStartPage;
        self.needLoadMore = YES;
        self.canLoadMore = YES;
        self.loadMoreUsePage = YES;

        self.lastRequestDate = kErrorInt;
    }
    return self;
}

-(void)loadPageData:(void (^)(AFHTTPRequestOperation *operation, id responseObject, BOOL haveNextPage))success
            failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{
    self.loadingStatus = LOADING_START;
    
    if (self.loadMoreUsePage)
    {
        if([self.urlPath rangeOfString:@"?"].location == NSNotFound)
        {
            self.urlPath = [NSString stringWithFormat:@"%@?page=%@",self.urlPath,INT_TO_STRING(self.requestPage)];
        }
        else
        {
            self.urlPath = [NSString stringWithFormat:@"%@&page=%@",self.urlPath,INT_TO_STRING(self.requestPage)];
        }
    }
    else
    {
        if([self.urlPath rangeOfString:@"?"].location == NSNotFound)
        {
          self.urlPath = [NSString stringWithFormat:@"%@?page=%@",self.urlPath,self.strLastId];
        }
        else
        {
           self.urlPath = [NSString stringWithFormat:@"%@&page=%@",self.urlPath,self.strLastId];
        }
    }

    [[HttpClient manager] GET:self.urlPath
                    parameters:self.parameters
                       success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         self.loadingStatus = LOADING_SUCCESS;
         if (self.needLoadMore)
         {
             //分页时处理结果-
             if (([self.strLastId intValue] == 0 && !self.loadMoreUsePage) || (self.requestPage == kLoadMoreStartPage && self.loadMoreUsePage))
             {
                 [self.dataArray removeAllObjects];
             }
            
             if ([self.strDataKey isEqualToString:requestDataKey])
             {
                 id obj = [responseObject objectForKey:requestDataKey];
                 [self parseData:obj];
                
                 if ([obj isKindOfClass:[NSArray class]] && [obj allObjects].count > 0)   self.canLoadMore = YES;
                 else  self.canLoadMore = NO;
                 
                // NSLog(@"==%d",self.canLoadMore);
             }else
             {
                 id dicData = [responseObject objectForKey:requestDataKey];
                 if ([dicData isKindOfClass:[NSDictionary class]])
                 {
                    id obj = [dicData objectForKey:self.strDataKey];
                    [self parseData:obj];
                 
                   if ([obj isKindOfClass:[NSArray class]] && [obj allObjects].count > 0) self.canLoadMore = YES;
                    else  self.canLoadMore = NO;
                 }
                 
                //NSLog(@"==%d",self.canLoadMore);
             }
            
             if (success) success(operation, responseObject, self.canLoadMore);
         }
         else
         {
             [self.dataArray removeAllObjects];
             if ([self.strDataKey isEqualToString:requestDataKey])
             {
                 [self parseData:[responseObject objectForKey:requestDataKey]];
             }else
             {
                 id dicData = [responseObject objectForKey:requestDataKey];
                 if ([dicData isKindOfClass:[NSDictionary class]])
                 {
                   [self parseData:[dicData objectForKey:self.strDataKey]];
                 }
             }
             if (success) success(operation, responseObject, NO);
         }

     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         self.loadingStatus = LOADING_FAILED;
         if (failure)
         {
             failure(operation, error);
         }
     }];

}

//上拉加载统一处理
-(void)parseData:(id)dataObj
{
    [super parseData:dataObj];
    
    if (self.needLoadMore)
    {
        if ([dataObj isKindOfClass:[NSArray class]])
        {
            if ([dataObj count] > 0) {
                if (self.loadMoreUsePage)
                {
                    self.self.requestPage++;
                }
                else
                {
                    NSMutableDictionary* data = [dataObj objectAtIndex:[dataObj count]-1];
                    self.strLastId = NullObjToString([data objectForKey:@"id"]);
                }
            }
        }
    }
    
}

@end
